var app = getApp();
const qiniu = require("../../utils/qiniuUploader");
Page({
    data: {
        order_id: '',
        status: true,
        license: '',
        files_num: 0, // 图片id
        qiniu_token: "", // 七牛token 
        qiniu_link: "",
        imgs: [],
        data_top: '',
        trade_no: '',
        tracking_company: '',
        tracking_id: '',
        items: [
            {name: 'yes',value: '已发货'}
        ],
        member: [],
        objectArray: [
            {id: 0, name: '韵达快递'},
            {id: 1, name: '申通快递'},
            {id: 2, name: '顺风快递'},
            {id: 3, name: '圆通快递'}
        ],
        index: 0,

        bindPickerChange(e) {
            console.log('picker发送选择改变，携带值为', e.detail.value)
            this.setData({
                index: e.detail.value
            })
        },
    },
    tracking_number(e) {
        this.setData({
            tracking_number: e.detail.value
        })
    },
    bindPickerChange(e) {
        var member = this.data.member;
        this.setData({
            tracking_company: member[e.detail.value].name,
            tracking_id: member[e.detail.value].id,
            index: e.detail.value
        })
    },
    // 上传图片
    uploadImg(e) {
        var self = this;
        var imgs = this.data.imgs;
        var license = this.data.license;

        var qiniu_link = this.data.qiniu_link;
        var qiniu_token = this.data.qiniu_token;
        var uploadURL = "https://upload.qiniup.com";

        wx.chooseImage({
            count: 9,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                const tempFilePaths = res.tempFilePaths;
                for (var i = 0; i < tempFilePaths.length; i++) {
                    var file = tempFilePaths[i];

                    var uploader = qiniu.upload(file, (res) => {
                        var url = res.imageURL;
                        var id = res.hash;
                        if (imgs.length >= 9) {
                            return false;
                        }
                        imgs.push({
                            id: id,
                            url: url
                        })
                        self.setData({
                            imgs: imgs,
                        })
                    }, (error) => {
                        console.log('error: ' + error);
                    }, {
                        uploadURL: uploadURL,
                        domain: qiniu_link,
                        uptoken: qiniu_token,
                    });
                }
            }
        })
    },
    // 删除图片
    deleteImg(e) {
        var self = this;
        var type = e.target.dataset.type
        var id = e.target.dataset.id;
        var imgs = this.data.imgs;
        var license = this.data.license;
        console.log(imgs)
        imgs.map((item, index) => {
            if (item.id == id) {
                console.log(item.id, index)
                imgs.splice(index, 1)
            }
        })
        this.setData({
            imgs: imgs
        })
    },
    // 获取七牛所需数据
    getQiniuInfo() {
        var self = this;
        var url = "/upload/token";

        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token
                    },
                    success: function(res) {
                        var datas = res.data;
                        if (datas.code == 1) {
                            self.setData({
                                qiniu_token: datas.data.token,
                                qiniu_link: datas.data.url
                            })
                        }
                    }
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getQiniuInfo();
        let trade_no = options.trade_no;
        this.setData({
            
            order_id: options.id,
        })
        var that = this;
        console.log(app.url.requestUrl + '/order/cred')
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/order/cred',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        order_id: options.id
                    },
                    success: function(res) {
                        if (res.data.code == 1) {
                            that.setData({
                                data_top: res.data.data.goods_iterm,
                                member: res.data.data.companys,
                                tracking_company: res.data.data.companys[0].name,
                                tracking_id: res.data.data.companys[0].id,
                                trade_no: res.data.data.trade_no
                            })
                        }else{
                            wx.showToast({
                                title: res.data.msg,
                            })
                        }
                    }
                })
            }
        })
    },

    verifyForm: function(e) {
        var img = [];
        this.data.imgs.map(function(item, index) {
            img.push(item.url);
        })
        // var tracking_imgs = img.split(",")
        // data.imgs = img.join(",");
        var that = this;
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/order/savecred',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        tracking_number: that.data.tracking_number,
                        tracking_company: that.data.tracking_company,
                        tracking_id: that.data.tracking_id,
                        tracking_imgs: img.toString(),
                        order_id: that.data.order_id
                    },
                    header: {
                        'content-type': 'application/json'
                    },
                    success: function(res) {
                        console.log(res.data)
                        if (res.data.code == 1) {

                            wx.navigateBack({
                                delta: 1
                            })
                            // that.onLoad()

                            that.setData({
                                masksell: false,
                                maskoverly: false,
                            })
                            wx.showToast({
                                title: res.data.msg,
                                icon: '',
                                duration: 2000,
                                mask: true
                            })
                        } else {
                            wx.showToast({
                                title: res.data.msg,
                                icon: '../img/icon-prompt.png',
                                duration: 2000,
                                mask: true
                            })
                        }

                    }
                })
            }
        })
    }

})